package work.bio.count_append;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import core.util.AddressUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import work.MemberUtil;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

public class Demo {
    private interface Setting {
        String token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJtYW5hZ2VyIiwiY2xpZW50VHlwZSI6InVzZXIiLCJ0ZW5hbnRJZCI6ImxvcmVhbCIsImV4cCI6MTY5NDc2OTczMiwianRpIjoiNGk0WFdGR25UYW1HOWgxV0JKVDM2NSJ9.HppiQ4XC-jkgF7jClovlsAyIHs-UnGGFTtya-HOMC7iJl9PIx_kF61XbVxRxdsHaWi30qoTaQoGWuFUYWWkYrQ";
        String baseUrl = "https://socialcrm-admin-api.biotherm.com.cn/gateway/api";
        String sheetName = "Counter Information";
        String basePath = "C:\\work-records\\Apple\\l-bio门店添加\\2023-09-15";
        String filePath = basePath + "\\原文件.xlsx";

    }

    public static void main(String[] args) {
        EasyExcel.read(Setting.filePath, ExcelObject.class, new Process())
                .sheet(Setting.sheetName)
                .doRead();
    }

    @Data
    public static class ExcelObject {

        @ExcelProperty(index = 1)
        private String id;
        @ExcelProperty(index = 2)
        private String name;
        @ExcelProperty(index = 3)
        private String tel;
        @ExcelProperty(index = 7)
        private String address;

        public MemberUtil.Counter toCounter(double x, double y) {
            MemberUtil.Counter counter = new MemberUtil.Counter();
            counter.setId(this.id);
            counter.setCode(this.id);
            counter.setName(name);
            counter.setMarsCode(this.id);
            counter.setType("physicalStore");
            counter.setBizType(List.of("common"));
            counter.setPhone(tel);
            counter.setTelPhone(tel);
            counter.setLongitude(x);
            counter.setLatitude(y);
            counter.setAddress(address);
            counter.setActive(true);
            counter.setCounterStatus("onBusiness");
            return counter;
        }
    }

    @Slf4j
    private static class Process implements ReadListener<ExcelObject> {
        private final List<ExcelObject> addressErr = new CopyOnWriteArrayList<>();
        private final List<ExcelObject> appendCounter = new CopyOnWriteArrayList<>();

        private final List<String> allIds = new CopyOnWriteArrayList<>();

        int count = 0;

        @Override
        public void invoke(ExcelObject excelObject, AnalysisContext analysisContext) {
            log.info("【门店检查】第{}个，counter->{}", ++count, JSONUtil.toJsonStr(excelObject));
            allIds.add(excelObject.getId());
            JSONObject entries = MemberUtil.countFindItem(excelObject.getId(), Setting.baseUrl, Setting.token);
            if (Objects.nonNull(entries)) {
                log.info("【门店检查】门店非空->{}", entries.toString());
                return;
            }
            AddressUtil.AddressEntity address = AddressUtil.findByAddress(excelObject.address);
            if (Objects.isNull(address)) {
                log.error("【门店添加】未找到地址 counter->{}", JSONUtil.toJsonStr(excelObject));
                addressErr.add(excelObject);
                return;
            }
            MemberUtil.Counter counter = excelObject.toCounter(address.getX(), address.getY());
            log.info("【门店添加】门店->{}", JSONUtil.toJsonStr(counter));
            MemberUtil.counterCreate(counter, Setting.baseUrl, Setting.token);
            appendCounter.add(excelObject);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            log.info("处理完毕,地址异常用户->{},添加用户->{},全部数量->{}", addressErr.size(), appendCounter.size(), allIds.size());
            EasyExcel.write(Setting.basePath + "/addressErr.xlsx").sheet().doWrite(addressErr);
            EasyExcel.write(Setting.basePath + "/appendCounter.xlsx").sheet().doWrite(appendCounter);
            FileUtil.writeString(JSONUtil.toJsonStr(allIds), Setting.basePath + "/allIds.json", Charset.defaultCharset());
        }
    }
}
